use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Article::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Article::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Article::Slug).string().not_null().unique_key())
                    .col(ColumnDef::new(Article::Status).string().not_null())
                    .col(ColumnDef::new(Article::FeaturedImageUrl).string())
                    .col(ColumnDef::new(Article::PublishedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(Article::CategoryId).uuid())
                    .col(ColumnDef::new(Article::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(Article::UpdatedAt).timestamp_with_time_zone().not_null())
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk-article-category_id")
                            .from(Article::Table, Article::CategoryId)
                            .to(Category::Table, Category::Id)
                            .on_delete(ForeignKeyAction::SetNull),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Article::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Category {
    Table,
    Id,
}

#[derive(DeriveIden)]
enum Article {
    Table,
    Id,
    Slug,
    Status,
    FeaturedImageUrl,
    PublishedAt,
    CategoryId,
    CreatedAt,
    UpdatedAt,
}